import * as types from '../types';
import * as helps from '@/utils/helps';

/**
 * 地图统计模块
 */
const statisticApp = {
  state: {
    modules: [], //模块列表
    leftModules: [], //左侧模块列表(固定)
    bottomModules: [], //底部模块列表(动态)
    leftPanelState: 0, //左侧面板开关状态，0:关闭，1：打开
    bottomPanelState: 0, //底部面板开关状态，0:关闭，1：打开，2:折叠
    map: null,
    appOptions: {},
  },
  getters: {
    //激活的左面板模块
    activedLeftModule: state => state.leftModules.find(p => p.actived),
    //激活的底部面板模块
    activedBottomModule: state => state.bottomModules.find(p => p.actived),
  },
  mutations: {
    //初始化面板管理模块
    [types.INIT_PANEL_MODULES](state, modules) {
      //获取所有模块
      state.modules = Object.keys(modules)
        .map(p => {
          let m = modules[p];
          return {
            id: m.name,
            name: m.name,
            title: m.title,
            icon: m.icon,
            position: m.position,
            order: m.order,
            visible: m.visible,
            actived: false,
            options: {},
          };
        })
        .filter(p => p.visible);
      //获取左面板模块
      state.leftModules = state.modules
        .filter(p => p.position == 'left')
        .sort((a, b) => a.order > b.order);
      state.bottomModules = [];
      state.leftPanelState = 0;
      state.bottomPanelState = 0;
    },
    //切换左面板模块状态
    [types.TOGGLE_LEFT_MODULE](state, id) {
      let item = state.leftModules.find(p => p.id == id);
      if (item) {
        item.actived = !item.actived;
        item.actived && state.leftModules.forEach(p => p.id != item.id && (p.actived = false));
        state.leftPanelState = item.actived ? 1 : 0;
      }
    },
    //设置左侧面板开关状态
    [types.SET_LEFT_PANEL_STATE](state, panelState) {
      state.leftPanelState = panelState;
      panelState == 0 && state.leftModules.forEach(p => (p.actived = false));
    },
    //新增底部模块
    [types.ADD_BOTTOM_MODULE](state, { type, id, title, order, options }) {
      let moduleInfo = state.bottomModules.find(p => p.id == id);
      if (moduleInfo) {
        moduleInfo.actived = true;
        moduleInfo.title = title || moduleInfo.title;
        moduleInfo.options = options;
        state.bottomPanelState = 1;
        return;
      }
      let moduleMata = state.modules.find(p => p.name == type && p.position == 'bottom');
      if (moduleMata) {
        moduleInfo = helps.deepCopy(moduleMata);
        moduleInfo.id = id;
        moduleInfo.title = title || moduleInfo.title;
        moduleInfo.options = options;
        moduleInfo.order = order === undefined ? state.bottomModules.length : order;
        state.bottomModules.splice(moduleInfo.order, 0, moduleInfo);
        state.bottomPanelState = 1;
      }
    },
    //删除底部模块
    [types.REMOVE_BOTTOM_MODULE](state, id) {
      let index = -1;
      for (let i = 0; i < state.bottomModules.length; i++) {
        if (state.bottomModules[i].id == id) {
          index = i;
          break;
        }
      }
      if (index >= 0) {
        state.bottomModules.splice(index, 1);
        let count = state.bottomModules.length;
        if (count == 0) {
          state.bottomPanelState = 0;
        } else if (count == 1) {
          state.bottomModules[0].actived = true;
        } else {
          state.bottomModules[index].actived = true;
        }
      }
    },
    //激活底部模块状态
    [types.ACTIVE_BOTTOM_MODULE](state, id) {
      state.bottomModules.forEach(p => (p.actived = p.id == id));
      state.bottomPanelState = 1;
    },
    //关闭底部面板
    [types.SET_BOTTOM_PANEL_STATE](state, panelState) {
      state.bottomPanelState = panelState;
    },
    //更新模块设置参数
    [types.SET_MODULE_OPTIONS](state, { id, options }) {
      let item = state.leftModules.find(p => p.id == id);
      !item && (item = state.bottomModules.find(p => p.id == id));
      item && (item.options = options);
    },
  },
};

export default statisticApp;
